<?php
/**
 * This controller is uses to profile of textbook
 *
 * @author	huytbt
 * @date	2012-05-26
 * @version	1.0
 */
class ProfileController extends HController
{
	// Set action default for this controller
	public $defaultAction = 'index';

	/**
	 * This method is used to show profile
	 */
	public function actionIndex()
	{
		// Get current user
		Yii::import('application.modules.user.models.User');
		Yii::import('application.modules.user.models.UserProfiles');
		$user = User::model()->findByPk(Yii::app()->user->id);
		
		// Get user courses
		Yii::import('application.modules.textbooks.models.TextbookUserCourse');
		$userCoursesTaking = $user->courses(array('condition'=>'type=:type', 'params'=>array(':type'=>TextbookUserCourse::TYPE_TAKING)));
		$userCoursesTaken = $user->courses(array('condition'=>'type=:type', 'params'=>array(':type'=>TextbookUserCourse::TYPE_TAKEN)));
		
		// Get user books
		$books = $user->books;
		
		$this->render('index', array(
			'user' => $user,
			'userCoursesTaking' => $userCoursesTaking,
			'userCoursesTaken' => $userCoursesTaken,
			'books' => $books,
		));
	}

	/**
	 * This action is used to management courses of user
	 */
	public function actionCourses()
	{
		// Get current user
		Yii::import('application.modules.user.models.User');
		$user = User::model()->findByPk(Yii::app()->user->id);
		
		Yii::import('application.modules.textbooks.models.TextbookCourse');
		$model = new TextbookCourse;
		
		if(isset($_POST['TextbookCourse'])) {
			// Uncomment the following line if AJAX validation is needed
			$this->performAjaxValidation($model);
			
			$model->attributes = $_POST['TextbookCourse'];
			if ($model->validate()) {
				Yii::import('application.modules.textbooks.models.TextbookCourse');
				$course = TextbookCourse::model()->findByAttributes(array(
					'name' => $model->name,
					'department_id' => $model->department_id,
				));
				if (!$course) {
					$model->save();
					$course = $model;
				}
				if ($course) {
					Yii::import('application.modules.textbooks.models.TextbookUserCourse');
					$exists = TextbookUserCourse::model()->findByAttributes(array(
						'user_id' => $user->id,
						'course_id' => $course->id,
						'type' => isset($_POST['TextbookCourse']['type']) ? $_POST['TextbookCourse']['type'] : TextbookUserCourse::TYPE_TAKEN,
					));
					if (!$exists) {
						$userCourse = new TextbookUserCourse;
						$userCourse->user_id = $user->id;
						$userCourse->course_id = $course->id;
						$userCourse->type = isset($_POST['TextbookCourse']['type']) ? $_POST['TextbookCourse']['type'] : TextbookUserCourse::TYPE_TAKEN;
						$userCourse->save();
					}
				}
				
			}
		}
		
		// Get user departments
		Yii::import('application.modules.textbooks.models.TextbookUniversity');
		$universities = $user->universities;
		if ($universities)
			$university = $user->universities[0]->university;
		else
			$this->redirect(array('/textbooks/profile/universities'));
		Yii::import('application.modules.textbooks.models.TextbookDepartment');
		$departments = $university->departments;
		
		// Get user courses
		Yii::import('application.modules.textbooks.models.TextbookUserCourse');
		$userCoursesTaking = $user->courses(array('condition'=>'type=:type', 'params'=>array(':type'=>TextbookUserCourse::TYPE_TAKING)));
		$userCoursesTaken = $user->courses(array('condition'=>'type=:type', 'params'=>array(':type'=>TextbookUserCourse::TYPE_TAKEN)));
		
		// Register assets
		$baseScriptUrl = Yii::app()->assetManager->publish(dirname(__FILE__) . '/../assets');
		Yii::app()->clientScript->registerScriptFile($baseScriptUrl . '/js/jquery.h.textbooks.course.js');
		
		$this->render('courses', array(
			'model' => $model,
			'userCoursesTaking' => $userCoursesTaking,
			'userCoursesTaken' => $userCoursesTaken,
			'departments' => $departments,
		));
	}

	/**
	 * This method is used to remove course of user
	 */
	public function actionRemoveCourse($id)
	{
		// Get current user
		Yii::import('application.modules.user.models.User');
		$user = User::model()->findByPk(Yii::app()->user->id);
		
		Yii::import('application.modules.textbooks.models.TextbookUserCourse');
		$model = TextbookUserCourse::model()->findByAttributes(array(
			'id' => $id,
			'user_id' => $user->id,
		));
		
		if ($model) $model->delete();
		$this->redirect(array('/textbooks/profile/courses'));
	}

	/**
	 * This method is used to set default university for user
	 */
	public function actionUniversities($university_id = null)
	{
		// Get current user
		Yii::import('application.modules.user.models.User');
		$user = User::model()->findByPk(Yii::app()->user->id);
		
		Yii::import('application.modules.textbooks.models.TextbookUserUniversity');
		$model = new TextbookUserUniversity();
		
		if ($university_id) {
			$primary = TextbookUserUniversity::model()->findByAttributes(array(
				'user_id' => $user->id,
				'university_id' => $university_id,
			));
			if ($primary) {
				TextbookUserUniversity::model()->updateAll(array('is_primary'=>0), 'user_id=:user_id', array(':user_id'=>$user->id));
				$primary->is_primary = 1;
				if ($primary->save())
					$this->redirect(array('/textbooks/profile/universities'));
			}
		} else if(isset($_POST['TextbookUserUniversity'])) {
			// Uncomment the following line if AJAX validation is needed
			$this->performAjaxValidation($model);
			
			$model->attributes = $_POST['TextbookUserUniversity'];
			$model->user_id = $user->id;
			
			if ($model->validate()) {
				$exists = TextbookUserUniversity::model()->findByAttributes(array(
					'user_id' => $user->id,
					'university_id' => $model->university_id,
				));
				
				if (!$exists) $model->save();
			}
		}
		
		// Get user universities
		$userUnniversities = $user->universities(array('order'=>'universities.id ASC'));
		
		// Get list universities
		Yii::import('application.modules.textbooks.models.TextbookUniversity');
		$universities = TextbookUniversity::model()->findAll();
		
		$this->render('universities', array(
			'model' => $model,
			'universities' => $universities,
			'userUnniversities' => $userUnniversities,
		));
	}

	/**
	 * This method is used to remove course of user
	 */
	public function actionRemoveUniversity($id)
	{
		// Get current user
		Yii::import('application.modules.user.models.User');
		$user = User::model()->findByPk(Yii::app()->user->id);
		
		Yii::import('application.modules.textbooks.models.TextbookUserUniversity');
		$model = TextbookUserUniversity::model()->findByAttributes(array(
			'id' => $id,
			'user_id' => $user->id,
		));
		
		if ($model) $model->delete();
		$this->redirect(array('/textbooks/profile/universities'));
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if (Yii::app()->request->isAjaxRequest)
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
